{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deepseek在线版连接\n",
    "硅基流动：\n",
    "sk-wjailvtcopklijnzpqouifnsezseczledyvaccccidzkxskc\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"id\":\"0194dfeca3e4207a500cb7e65a6c6b65\",\"object\":\"chat.completion\",\"created\":1738923615,\"model\":\"deepseek-ai/DeepSeek-R1\",\"choices\":[{\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\"深度求索智能助手，高效准确助您解决问题。\",\"reasoning_content\":\"好的，用户让我用20个字以内介绍自己。首先，我需要明确自己的身份和功能。我是由深度求索公司开发的智能助手DeepSeek-R1，专注于帮助用户提供高效、准确的信息和服务。接下来要检查字数限制，确保不超过20个字。可能需要简化描述，去掉不必要的修饰词。比如“高效、准确”可以保留，突出核心优势。然后结构要简洁，直接说明身份和主要功能。最后组合起来看看是否符合要求：“深度求索智能助手，高效准确助您解决问题。”数一下刚好20个字。再检查是否有更合适的词汇替换，比如“助您”是否必要？或者可以更简洁地说“提供高效准确的信息服务”。但可能超过字数。再调整：“智能助手DeepSeek-R1，高效准确助您解忧。”这样也是20字。不过用户可能更关注功能和公司，所以保留公司名称更好。最终确定：“深度求索智能助手，高效准确助您解决问题。”符合所有要求。\"},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":12,\"completion_tokens\":218,\"total_tokens\":230},\"system_fingerprint\":\"\"}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "url = \"https://api.siliconflow.cn/v1/chat/completions\"\n",
    "api_key = \"sk-wjailvtcopklijnzpqouifnsezseczledyvaccccidzkxskc\"\n",
    "payload = {\n",
    "    \"model\": \"deepseek-ai/DeepSeek-R1\",\n",
    "    #\"model\": \"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B\",\n",
    "    \"messages\": [\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"用20个字以内介绍下你自己\"\n",
    "        }\n",
    "    ],\n",
    "    \"stream\": False,\n",
    "    \"max_tokens\": 512,\n",
    "    \"stop\": [\"null\"],\n",
    "    \"temperature\": 0.7,\n",
    "    \"top_p\": 0.7,\n",
    "    \"top_k\": 50,\n",
    "    \"frequency_penalty\": 0.5,\n",
    "    \"n\": 1,\n",
    "    \"response_format\": {\"type\": \"text\"}\n",
    "}\n",
    "headers = {\n",
    "    \"Authorization\": f\"Bearer {api_key}\",\n",
    "    \"Content-Type\": \"application/json\"\n",
    "}\n",
    "\n",
    "response = requests.request(\"POST\", url, json=payload, headers=headers)\n",
    "\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "def llm(content):\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def llm(prompt):\n",
    "    url = \"https://api.siliconflow.cn/v1/chat/completions\"\n",
    "    api_key = \"sk-wjailvtcopklijnzpqouifnsezseczledyvaccccidzkxskc\"\n",
    "    payload = {\n",
    "        #\"model\": \"deepseek-ai/DeepSeek-R1\",\n",
    "        \"model\": \"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B\",\n",
    "        \"messages\": [\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": prompt\n",
    "            }\n",
    "        ],\n",
    "        \"stream\": False,\n",
    "        \"max_tokens\": 8196,\n",
    "        \"stop\": [\"null\"],\n",
    "        \"temperature\": 0.7,\n",
    "        \"top_p\": 0.7,\n",
    "        \"top_k\": 50,\n",
    "        \"frequency_penalty\": 0.5,\n",
    "        \"n\": 1,\n",
    "        \"response_format\": {\"type\": \"text\"}\n",
    "    }\n",
    "    headers = {\n",
    "        \"Authorization\": f\"Bearer {api_key}\",\n",
    "        \"Content-Type\": \"application/json\"\n",
    "    }\n",
    "\n",
    "    response = requests.request(\"POST\", url, json=payload, headers=headers)\n",
    "    response_data = response.json()\n",
    "    reasoning_content = response_data[\"choices\"][0][\"message\"][\"reasoning_content\"]\n",
    "    response_content = response_data[\"choices\"][0][\"message\"][\"content\"]\n",
    "\n",
    "    return reasoning_content, response_content\n",
    "\n",
    "\n",
    "# 测试代码\n",
    "prompt = \"\"\"\n",
    "## 目标\n",
    "你是一个专业的python数据科学家,擅长各种数据分析任务，请回答我的问题：\n",
    "你觉得我们应该如何分析表中的数据？有哪些维度？\n",
    "\n",
    "## 工具\n",
    "df_get = Tool(\n",
    "    name=\"df_get\",\n",
    "    description=\"你可以获取到整个数据表，数据形式为pandas的DataFrame.\",\n",
    "    func=dataframe.get,\n",
    ")\n",
    "\n",
    "repl_tool = Tool(\n",
    "    name=\"python_repl\",\n",
    "    description=\"A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\",\n",
    "    func=python_repl.run,\n",
    ")\n",
    "\n",
    "## df中的数据示例\n",
    "df.shape:(12, 2)\n",
    "df.head:\n",
    "月份  温度\n",
    "0   1  18\n",
    "1   2  19\n",
    "2   3  20\n",
    "df.dtypes:\n",
    "月份    int64\n",
    "温度    int64\n",
    "dtype: object\n",
    "\"\"\"\n",
    "reasoning_content, response_content = llm(prompt)\n",
    "print(\"<think>\\n\", reasoning_content)\n",
    "print(\"\\n<response>\\n\", response_content)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 百炼满血R1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [404]>\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "def llm_r1(prompt):\n",
    "    url = \"https://dashscope.aliyuncs.com/compatible-mode/v1\"\n",
    "    api_key = \"sk-0e687ddcf0164a6fb66c1096447223c4\"\n",
    "    payload = {\n",
    "        \"model\": \"deepSeek-r1\",\n",
    "        #\"model\": \"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B\",\n",
    "        \"messages\": [\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": prompt\n",
    "            }\n",
    "        ],\n",
    "        \"stream\": False,\n",
    "        \"max_tokens\": 8196,\n",
    "        \"stop\": [\"null\"],\n",
    "        \"temperature\": 0.7,\n",
    "        \"top_p\": 0.7,\n",
    "        \"top_k\": 50,\n",
    "        \"frequency_penalty\": 0.5,\n",
    "        \"n\": 1,\n",
    "        \"response_format\": {\"type\": \"text\"}\n",
    "    }\n",
    "    headers = {\n",
    "        \"Authorization\": f\"Bearer {api_key}\",\n",
    "        \"Content-Type\": \"application/json\"\n",
    "    }\n",
    "\n",
    "    response = requests.request(\"POST\", url, json=payload, headers=headers)\n",
    "    #response_data = response.json()\n",
    "    print(response)\n",
    "    #reasoning_content = response_data[\"choices\"][0][\"message\"][\"reasoning_content\"]\n",
    "    #response_content = response_data[\"choices\"][0][\"message\"][\"content\"]\n",
    "\n",
    "    #return reasoning_content, response_content\n",
    "\n",
    "\n",
    "# 测试代码\n",
    "prompt = \"\"\"\n",
    "## 目标\n",
    "你是一个专业的python数据科学家,擅长各种数据分析任务，请回答我的问题：\n",
    "你觉得我们应该如何分析表中的数据？有哪些维度？\n",
    "\n",
    "## 工具\n",
    "df_get = Tool(\n",
    "    name=\"df_get\",\n",
    "    description=\"你可以获取到整个数据表，数据形式为pandas的DataFrame.\",\n",
    "    func=dataframe.get,\n",
    ")\n",
    "\n",
    "repl_tool = Tool(\n",
    "    name=\"python_repl\",\n",
    "    description=\"A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\",\n",
    "    func=python_repl.run,\n",
    ")\n",
    "\n",
    "## df中的数据示例\n",
    "df.shape:(12, 2)\n",
    "df.head:\n",
    "月份  温度\n",
    "0   1  18\n",
    "1   2  19\n",
    "2   3  20\n",
    "df.dtypes:\n",
    "月份    int64\n",
    "温度    int64\n",
    "dtype: object\n",
    "\"\"\"\n",
    "llm_r1(prompt)\n",
    "#reasoning_content, response_content = llm_r1(prompt)\n",
    "#print(\"<think>\\n\", reasoning_content)\n",
    "#print(\"\\n<response>\\n\", response_content)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
